/**
 * @file 顶部进度条
 * @author liaoxuezhi@baidu.com
 */
/* eslint fecs-indent: [0, "space", 2, 2] */

import * as assign from 'object-assign';
import {show, hide, isVisible, Component} from 'react-progress-2';

export default function ajaxProgress(next) {
  return (reducer, initialState) => {
    const store = next(reducer, initialState);
    let started = false;

    return assign({}, store, {
      dispatch: action => {
        const type =  action.type;

        if (Component.instance && /^REQ/.test(type) && !isVisible()) {
          started = true;
          show();
        } else if (started && /^REC/.test(type) && isVisible()) {
          started = false;
          hide();
        }

        store.dispatch(action);
        return action;
      }
    });
  };
}
